import sys

import config
import arduino
import sentinel_cmt

from Queue import PriorityQueue

commandQ = PriorityQueue()

def main():
  try:
    settings = config.Config()
    print "Configured for", settings.platform_os
    
    ard = arduino.Arduino(settings)

    conn_result = ard.connect()
    
    if not conn_result[0]:
      raise IOError("Unable to connect to Arduino")
    
    print "Connected to Arduino on serial port:", conn_result[1]
    
    init_result = ard.initialize(settings.image_width)
    
    if int(init_result[0]) != 0 or int(init_result[1]) == 0:
      print "Failed to initialize Arduino", init_result
    else:
      print "Initialization complete:", init_result

  except KeyboardInterrupt:
    print "Shutdown requested...exiting."
  except IOError, e:
    print e
  except Exception, e:
    raise
  else:
    ard.disconnect()
    sys.exit(0)

if __name__ == "__main__":
  main()
